package com.ruoyi.busi.controller;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.busi.domain.BusiCompany;
import com.ruoyi.busi.domain.bo.BusiCompanyBo;
import com.ruoyi.busi.domain.vo.BusiCompanyImportVo;
import com.ruoyi.busi.domain.vo.BusiCompanyVo;
import com.ruoyi.busi.service.IBusiCompanyService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 企业Controller
 *
 * @author qibu
 * @date 2024-11-10
 */
@Validated
@Api(value = "企业控制器", tags = {"企业管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/busi/company")
public class BusiCompanyController extends BaseController {

    private final IBusiCompanyService iBusiCompanyService;

    /**
     * 查询企业列表
     */
    @ApiOperation("查询企业列表")
    @PreAuthorize("@ss.hasPermi('busi:company:list')")
    @GetMapping("/list")
    public TableDataInfo<BusiCompanyVo> list(@Validated(QueryGroup.class) BusiCompanyBo bo) {
        return iBusiCompanyService.queryPageList(bo);
    }

    /**
     * 导出企业列表
     */
    @ApiOperation("导出企业列表")
    @PreAuthorize("@ss.hasPermi('busi:company:export')")
    @Log(title = "企业", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(@Validated BusiCompanyBo bo, HttpServletResponse response) {
        List<BusiCompanyVo> list = iBusiCompanyService.queryList(bo);
        ExcelUtil.exportExcel(list, "企业", BusiCompanyVo.class, response);
    }

    /**
     * 获取企业详细信息
     */
    @ApiOperation("获取企业详细信息")
    @PreAuthorize("@ss.hasPermi('busi:company:query')")
    @GetMapping("/{id}")
    public AjaxResult<BusiCompanyVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Long id) {
        return AjaxResult.success(iBusiCompanyService.queryById(id));
    }

    /**
     * 新增企业
     */
    @ApiOperation("新增企业")
    @PreAuthorize("@ss.hasPermi('busi:company:add')")
    @Log(title = "企业", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody BusiCompanyBo bo) {
        return toAjax(iBusiCompanyService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改企业
     */
    @ApiOperation("修改企业")
    @PreAuthorize("@ss.hasPermi('busi:company:edit')")
    @Log(title = "企业", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody BusiCompanyBo bo) {
        return toAjax(iBusiCompanyService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除企业
     */
    @ApiOperation("删除企业")
    @PreAuthorize("@ss.hasPermi('busi:company:remove')")
    @Log(title = "企业" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return toAjax(iBusiCompanyService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

    @GetMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil.exportExcel(new ArrayList<>(), "企业数据", BusiCompanyImportVo.class, response);
    }

    @Log(title = "导入企业", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:user:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        List<BusiCompanyImportVo> userListVo = ExcelUtil.importExcel(file.getInputStream(), BusiCompanyImportVo.class);
        List<BusiCompany> userList = BeanUtil.copyToList(userListVo, BusiCompany.class);
        String operName = getUsername();
        String message = iBusiCompanyService.importCompany(userList, updateSupport, operName);
        return AjaxResult.success(message);
    }
}
